// @ts-nocheck
// import jsPDF from 'jspdf';
// import html2canvas from 'html2canvas';
import { urls } from '@/utils/request/upload';
const getCoverDom = (url?: string) => {
  //获取封面图片
  if (url) {
    return `<img  src="${urls + url + '?imageView2/2/w/80/h/80/format/webp'}" alt="">`;
  } else {
    return ``;
  }
};
const level2Html = (e: any) => {
  if (e.length === 0) {
    return;
  }
  let str = ``;
  e.forEach((element) => {
    str =
      str +
      `<div >
                  <h2>${element.text}</h2>
                  ${getCoverDom(element.coverUrl)}
                  <p>${element.remarks ? element.remarks : '暂无'}</p>
                  <div >
  
                  </div>
                  ${level3Html(element.children)}
              </div>`;
  });
  return str;
};
const level3Html = (e: any) => {
  if (e.length === 0) {
    return '<p></p>';
  }
  let str = ``;
  e.forEach((element) => {
    str =
      str +
      `<div >
                  <div class="level-3">
                      <h3>${element.text}</h3>
                      ${getCoverDom(element.coverUrl)} 
                      <p>${element.remarks ? element.remarks : '暂无'}</p>
                      <div class="exbit-box">
  
                      </div>
                  </div>
              </div>`;
  });
  return str;
};
const getExbitDom = (e: any) => {
  if (e.length === 0) {
    return '<p></p>';
  }
  let str = ``;
  e.forEach((element) => {
    str =
      str +
      `<div >
                      <div class="level-3">
                          <h3>${element.text}</h3>
                          ${getCoverDom(element.coverUrl)} 
                          <p>${element.remarks ? element.remarks : '暂无'}</p>
                          <div class="exbit-box">
      
                          </div>
                      </div>
                  </div>`;
  });
};
const exportPdfContext = async (moduleLists: any, title?: string) => {
  const pdf = new jsPDF();

  let str = `   
    
      `;
  moduleLists.forEach((level) => {
    str =
      str +
      `
            <div >
                <h1>${level.text}</h1>  
                ${getCoverDom(level.coverUrl)}
                 
                <p>${level.remarks ? level.remarks : '暂无'}</p>
                <div class="exbit-box">
     
                </div>
                ${level.children ? level2Html(level.children) : '<p></p>'}
            </div>
        `;
    // ${level.children ? level2Html(level.children) : '<p></p>'}
    // ${level2Html(level.children)}
  });

  const virtualHtml = `<div id="pdf-daying-box"  style="display: flex;
                flex-direction: column;
                padding:'0 50px'; 
                width: 100vw;">${str}</div>`;
  console.log(virtualHtml);
  const div = document.createElement('div');
  div.innerHTML = virtualHtml;
  document.body.appendChild(div);
  const container = document.getElementById('pdf-daying-box');
  const scale = 1;
  setTimeout(() => {
    html2canvas(container, { useCORS: true, scale }).then((canvas) => {
      const contentWidth = canvas.width / scale;
      const contentHeight = canvas.height / scale;
      const PDF = new jsPDF('p', 'pt', [contentWidth, contentHeight]);
      const pageData = canvas.toDataURL('image/jpeg', 1.0);
      PDF.addImage(pageData, 'JPEG', 0, 0, contentWidth, contentHeight);
      PDF.save('ssssss' + '.pdf');
      document.body.removeChild(div);
    });
  }, 300);

  // const div = document.createElement('div');
  // div.innerHTML = virtualHtml;
  // document.body.appendChild(div);
  // const container = document.getElementById('pdf-daying-box');
  // html2canvas(container, { useCORS: true }).then((canvas) => {
  //   // 创建一个空的jsPDF实例

  //   // 使用html2canvas将HTML结构渲染到canvas上

  //   // 将canvas转换为图片数据URL
  //   const imgData = canvas.toDataURL('image/png');
  //   console.log(imgData);
  //   // 添加图片到PDF
  //   pdf.addImage(imgData, 'PNG', 10, 10);

  //   // 保存PDF文件
  //   pdf.save('example.pdf');
  //   setTimeout(() => {
  //     document.body.removeChild(div);
  //   }, 1000);
  // });
};
// export { exportPdfContext };
